﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DisplayBoardInfo.Client.Forms
{
    public partial class LoginForm : Form
    {
        private Network.Messages.Content.Hello helloObject;
        private Network.Messages.Content.User userObject;
        private NetworkClientClass networkCC;

        public Network.Messages.Content.User User { get { return userObject; } }

        public LoginForm(NetworkClientClass networkCC)
        {
            InitializeComponent();
            this.networkCC = networkCC;
            helloObject = new Network.Messages.Content.Hello();
        }

        void MessageRecived(object sender, Network.MessageEventArgs args)
        {
            Cursor = Cursors.Arrow;
            loginButton.Enabled = true;
            try
            {
                Network.Messages.NetworkMessage recieved = args.Message;
                if (recieved.Type == Network.Messages.NetworkMessageTypes.Hello)
                {
                    this.userObject = (Network.Messages.Content.User)recieved.Content;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;   
                }
                else if (recieved.Type == Network.Messages.NetworkMessageTypes.Error)
                {
                    MessageBox.Show(recieved.Content.ToString(), "Błąd logowania",
                    MessageBoxButtons.OK, MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1); 
                }
                else
                {
                    // dostałem nie to czego się spodziewałem
                }
            }
            catch (Exception)
            {
                // TODO
                throw;
            }
            
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            if (this.loginTB.Text.Count() > 0) // Dodać inne warunki
            {
                if (this.passTB.Text.Count() > 0)
                {
                    Cursor = Cursors.WaitCursor;
                    loginButton.Enabled = false;
                    helloObject.Username = this.loginTB.Text;
                    helloObject.Password = this.passTB.Text;

                    networkCC.Send(new Network.Messages.NetworkMessage(Network.Messages.NetworkMessageTypes.Hello, helloObject));
                    networkCC.C.MessageRecived += new Network.Client.MessageRecivedHandler(MessageRecived);
                }
                else
                {
                    MessageBox.Show("Nie wprowadzono hasła.", "Błąd logowania",
                    MessageBoxButtons.OK, MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1);
                }
            }
            else
            {
                MessageBox.Show("Nie wprowadzono loginu.", "Błąd logowania",
                    MessageBoxButtons.OK, MessageBoxIcon.Error, 
                    MessageBoxDefaultButton.Button1);
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
